package dbconn

import (
	"errors"
	"fmt"

	"iot-base/common/common"

	"github.com/aidarkhanov/nanoid/v2"
	"gorm.io/gorm"
)

//DBObjIDName 数据库对象
type DBObjIDName struct {
	//gorm.Model
	UID   string `gorm:"type:varchar(40);primaryKey" json:"uid"`
	SName string `gorm:"type:varchar(100);not null" json:"s_name"`
}

//OBJObjIID id为整形
type OBJObjIID struct {
	//gorm.Model
	UID int64 `gorm:"primaryKey;AUTO_INCREMENT;not null" json:"uid"`
}

//GetID 获取id
func (pObj *OBJObjIID) GetID() interface{} {
	return pObj.UID
}

//AutoID 自动生成id
func (pObj *OBJObjIID) AutoID() {

}

//DBObjIDer crud接口
type DBObjIDer interface {
	GetID() interface{}
	//GetName() string
	AutoID()
	//SetName(name string)
}

//SetName 设置名称
func (pObj *DBObjIDName) SetName(name string) {
	pObj.SName = name
}

//GetID 获取id
func (pObj *DBObjIDName) GetID() interface{} {
	return pObj.UID
}

//GetName 获取名称
func (pObj *DBObjIDName) GetName() string {
	return pObj.SName
}

//AutoID 自动生成id
func (pObj *DBObjIDName) AutoID() {
	v, _ := nanoid.New()
	pObj.UID = fmt.Sprintf("%v", v)
}

//DBObjIntIDName id为int64
type DBObjIntIDName struct {
	OBJObjIID
	SName string `gorm:"type:varchar(100);not null" json:"s_name"`
}

func isIDEmpty(pObj DBObjIDer) bool {
	id := pObj.GetID()
	nID, ok := id.(int)
	if ok {
		if nID == 0 {
			return true
		}
	} else {
		sID, _ := id.(string)
		if sID == "" {
			return true
		}
	}
	return false
}

//NewItem 新增节点
func NewItem(pObj DBObjIDer) error {
	if isIDEmpty(pObj) {
		pObj.AutoID()
		//return DBConnection.Create(pObj).Error
	}
	//var count int64
	//DBConnection.Model(pObj).Where("uid=?", pObj.GetID()).Count(&count)
	return DBConnection.Create(pObj).Error
}

//Update 更新
func Update(pObj DBObjIDer) error {
	//tableName := DBConnection.NewScope(pObj).TableName()
	//fmt.Println("------------update--------",tableName,info)

	if isIDEmpty(pObj) {
		return errors.New("id is empty")
	}
	//return DBConnection.Update()
	return DBConnection.Save(pObj).Error

}

//Delete 删除
func Delete(pObj DBObjIDer, id interface{}) error {
	//return DBConnection.Model(pObj).Where("uid=?",id).Delete().Error
	result, err := FindByID(pObj, id)
	if err == nil {
		return DBConnection.Unscoped().Delete(result).Error
	} else {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil
		}
	}
	return err
}

//FindByID 通过id查找
func FindByID(pObj DBObjIDer, id interface{}) (interface{}, error) {
	result, _ := common.CreateInstance(pObj) //make(map[string]interface{})
	//tableName := DBConnection.NewScope(pObj).TableName()
	err := DBConnection.Model(pObj).Where("uid=?", id).First(result).Error
	return result, err
}

//FindAll 查找所有
func FindAll(pObj DBObjIDer) (interface{}, error) {
	result := make([]map[string]interface{}, 0, 1)
	//result := common.CreateSlice(pObj)
	//tableName := DBConnection.NewScope(pObj).TableName()
	//result0 := map[string]interface{}{}

	err := DBConnection.Model(pObj).Order("uid").Find(&result).Error //First(&result0).Error
	//fmt.Println("&&&&&&&&",result,result.Kind())
	return result, err
}

func DeleteAll(pObj DBObjIDer) error {
	//tableName := DBConnection.NewScope(pObj).TableName()
	result, _ := common.CreateInstance(pObj)
	return DBConnection.Where("1=1").Delete(result).Error
}
